package com.zpark.controller;

import com.zpark.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/thy")
public class ThymeleafController {

    // th:text属性：文本替换属性
    @GetMapping("/th_text")
    public ModelAndView text(){
        ModelAndView vm = new ModelAndView();
        vm.setViewName("text"); // classpath:/templates/text.html
        vm.addObject("t_msg", "th:text属性");

        return vm;
    }

    // th:utext属性：文本替换(支持html)
    @GetMapping("/th_utext")
    public ModelAndView utext(){
        ModelAndView vm = new ModelAndView();
        vm.setViewName("utext"); // classpath:/templates/text.html
        vm.addObject("ut_msg", "<i>th:utext属性</i>");

        return vm;
    }

    // th:value：属性赋值，设置当前元素的value值
    @GetMapping("/th_value")
    public ModelAndView value(){
        ModelAndView vm = new ModelAndView();
        vm.setViewName("value"); // classpath:/templates/text.html
        vm.addObject("uname", "大锤");

        return vm;
    }

    // th:each：**遍历循环元素
    @GetMapping("/th_each")
    public ModelAndView each(){
        ModelAndView vm = new ModelAndView();

        List<User> lists = new ArrayList<>();
        User u1 = new User();
        u1.setId(89);
        u1.setUname("张三");
        u1.setPwd("zhangsan123456");
        u1.setAddress("北京");

        User u2 = new User();
        u2.setId(90);
        u2.setUname("李四");
        u2.setPwd("lisi789456");
        u2.setAddress("北京.海淀区");

        User u3 = new User();
        u3.setId(91);
        u3.setUname("大锤");
        u3.setPwd("shanghai123456");
        u3.setAddress("上海");

        User u4 = new User();
        u4.setId(92);
        u4.setUname("翠花");
        u4.setPwd("cuihua123456");
        u4.setAddress("西藏");

        lists.add(u1);
        lists.add(u2);
        lists.add(u3);
        lists.add(u4);

        vm.setViewName("each"); // classpath:/templates/text.html
        vm.addObject("users", lists);

        return vm;
    }

    // th:if：判断属性
    @GetMapping("/th_if")
    public ModelAndView thIf(){
        ModelAndView vm = new ModelAndView();
        vm.setViewName("if"); // classpath:/templates/text.html
        vm.addObject("if_msg", "R");

        return vm;
    }
}
